/**
 * 处理各类数据
 */

import { TMenu } from '@/api/sys/type'
import { RouteRecordRaw } from 'vue-router'

export interface IFilter {
  groupMenu: (menuList: TMenu[], parentId?: string) => TMenu[];

  mapMenu: (menuList: TMenu[], parentUrl: string) => Array<RouteRecordRaw>;
}

// 通过parentId 过滤菜单格式
export const groupMenu = (menuList: TMenu[], parentId = '0'): TMenu[] => {
  return menuList.filter((item: TMenu) => {
    if (item.parentId === parentId && item.type !== 2) {
      item.children = groupMenu(menuList, item.menuId)
      return item
    }
  })
}

// 组件处理
export const mapMenu = (menuList: TMenu[], parentUrl = 'view'): Array<RouteRecordRaw> => {
  return menuList.map((item: TMenu) => {
    const { url, menuName, icon, routerName, children } = item
    const meta = { title: menuName, icon }
    return {
      path: children.length !== 0 ? `/${url}` : url,
      meta,
      name: routerName,
      parentUrl,
      children: mapMenu(children, url),
      component: children.length !== 0 ? () => import('@/layout/index.tsx') : () => import('@/views/' + routerName)
    }
  }) as Array<RouteRecordRaw>
}
